<?xml version="1.0" encoding="UTF-8" ?>
<%@page import="com.baoyanghui.common.Constants"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.baoyanghui.common.Utils"%>
<%@page import="com.baoyanghui.common.DataChanger"%>
<%@page import="com.baoyanghui.common.InterfaceAccess"%>
<%@page import="com.baoyanghui.config.Configs"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="com.baoyanghui.model.MerchantPlace"%>
<%@page import="com.baoyanghui.model.Merchant"%>
<%@page import="net.sf.json.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% 
	if (Utils.empty((String)request.getSession().getAttribute(Configs.SESSION_USER_ID))
			|| (Constants.USER_TYPE_CUSTOMER == (Integer)request.getSession().getAttribute(Configs.SESSION_USER_TYPE))) {
		response.sendRedirect("login.jsp");
		return;
	}

	/* 取得错误信息 */
	String errorMessage = (String)session.getAttribute("error_message"); 
	session.removeAttribute("error_message");
	
	String divAddPlaceSattus = "hidden";
	String divAddPlaceCoopSattus = "hidden";
	String msgPlace = null;
	String msgCoopPlace = null;
	String tab = request.getParameter("tab");
	if ("add_place".equals(tab)) {
		divAddPlaceSattus = "visible";
		msgPlace = errorMessage;
	} else if ("add_place_coop".equals(tab)) {
		divAddPlaceCoopSattus = "visible";
		msgCoopPlace = errorMessage;
	} else {
		errorMessage = "";
	}
	
	JSONObject places = null;
	JSONObject profile = null;
	Merchant merchant = null;
	MerchantPlace place = null;
	List<MerchantPlace> placeList = null;
	List<MerchantPlace> verifiedPlaceList = null;
	Map<String, String> params = new HashMap<String, String>();
	List<String> coopIds = new ArrayList<String>();
	String imageName = "";
	
	try {
		request.setCharacterEncoding("UTF-8");
		params.put("merchant_id", (String)session.getAttribute(Configs.SESSION_USER_ID));
		params.put("usercode", (String)session.getAttribute(Configs.SESSION_USERCODE));

		profile = InterfaceAccess.callInterface("/merchant/get_profile", params);
		merchant = DataChanger.getMerchant(profile);
		places = InterfaceAccess.callInterface("/merchant/get_places", params);
		placeList = DataChanger.getPlaces(places);
		
		/* 取得所有已验证经营地址 */
		params.clear();
		places = InterfaceAccess.callInterface("/get_merchant_list", params);
		verifiedPlaceList = DataChanger.getPlaces(places);
		
		if (merchant.getPhotos() != null && merchant.getPhotos().size() > 0) {
			imageName = merchant.getPhotos().get(0);
		}
		
		for (int i = 0; placeList != null && i < placeList.size(); i++) {
			place = placeList.get(i);
			if (!"1".equals(place.getType())) {continue;}
			coopIds.add(place.getId());
		}
	} catch (Exception e) {
		response.sendRedirect("login.jsp");
		return;
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" type="text/css" href="../css/style.css"/>
		<script type="text/javascript" src="../js/jquery.js"></script>
		<script type="text/javascript" src="../js/manage_info.js"></script>
		<title>宝养汇</title>
	</head>
	<body onload="saveInitData();">
		<jsp:include page="../common/header.jsp"></jsp:include>
		<div id="div_content">
			<div style="width: 984px; height: 5px; background: url('<%= request.getContextPath() %>/image/bg_register_top.png');"></div>
			<div align="left" style="width: 984px; background: url('<%= request.getContextPath() %>/image/bg_register.png');">
				<h1 style="padding: 10px;">添加经营场所</h1>
				
				<form id="frm_manage_info" action="<%= request.getContextPath() %>/merchant/manage_info" method="post" style="padding-left: 40px;">
					<div align="left">
						<div style="width: 400px;">
							<h4>自主经营场所:</h4>
<%
	for (int i = 0; placeList != null && i < placeList.size(); i++) {
		place = placeList.get(i);
		if (!"0".equals(place.getType())) {continue;}
%>
							<span><%= place.getPlaceName() + "&nbsp;&nbsp;" + place.getAddress() + "&nbsp;&nbsp;" + place.getStatus() %></span><br/>
<%
	}
%>
							<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_add_place.png" alt="" style="cursor: pointer;"
								onclick="changeTab('div_add_place');"/>
						</div>
						<div style="width: 400px;">
							<h4>合作经营场所:</h4>
<%
	for (int i = 0; placeList != null && i < placeList.size(); i++) {
		place = placeList.get(i);
		if (!"1".equals(place.getType())) {continue;}
%>
							<span><%= place.getPlaceName() + "&nbsp;&nbsp;" + place.getAddress() + "&nbsp;&nbsp;" + place.getStatus() %></span><br/>
<%
	}
%>
							<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_add_place_coop.png" alt="" style="cursor: pointer;"
								onclick="changeTab('div_add_place_coop');"/>
						</div>
					</div>
					<input type="hidden" id="image_name" name="image_name"/>
					<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_complete.png" alt="" style="margin-left: 280px; margin-top: 100px;"
						onclick="document.getElementById('frm_manage_info').submit();"/>
				</form>
			</div>
			<div style="width: 984px; height: 7px; background: url('<%= request.getContextPath() %>/image/bg_register_bottom.png');"></div>
		</div>
		<div align="center" id="div_add_place" style="width:100%; position: absolute; left: 0px; top: 100px; visibility: <%= divAddPlaceSattus %>;">
			<div align="left" style="width: 700px; background-color: #777777; border: 1px solid gray;">
				<h3>添加自主经营场所</h3>
				<form id="frm_add_place" action="<%= request.getContextPath() %>/merchant/add_place" method="post" style="padding-left: 40px;">
					<div style="width: 300px; float: left;">
						<label>*场所名称：</label><input type="text" name="place_name" id="place_name" maxlength="20"/><br/>
						<label>*场所联系人：</label><input type="text" name="place_linkman" id="place_linkman" maxlength="20"/><br/>
						<label>*固定电话：</label><input type="text" id="office_phone" name="office_phone" maxlength="20"/><br/>
						<label>*场所地址：</label><input type="text" name="address" id="address" maxlength="50"/><br/>
						<label>*靠近：</label><input type="text" name="road_nearby" id="road_nearby" maxlength="50"/><br/>
					</div>
					<div style="width: 300px; float: right;">
						<label>手机号：</label><input type="text" name="mobile" id="mobile" maxlength="20"/><br/>
						<label>营业项目：</label><input type="text" name="business_item" id="business_item"/><br/>
					</div>
					<div style="clear: both;"></div>
					<div align="center">
						<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_confirm.png" alt="" style="margin-top: 20px;" onclick="document.getElementById('frm_add_place').submit();"/>
						<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_cancel.png" alt="" style="margin-top: 20px;"
							onclick="document.getElementById('div_add_place').style.visibility = 'hidden';"/>
					</div>
					<span id="error_message"><%= msgPlace == null ? "" : msgPlace %></span>
				</form>
			</div>
		</div>
		<div align="center" id="div_add_place_coop" style="width:100%; position: absolute; left: 0px; top: 100px; visibility: <%= divAddPlaceCoopSattus %>;">
			<div align="left" style="width: 700px; background-color: #777777; border: 1px solid gray;">
				<h3>添加合作经营场所</h3>
				<form style="padding-left: 40px;">
					<input type="text" id="search_keyword"/>
					<input class="fade_effect" id="btn_search_place" type="button" value="查找"/>
				</form>
				<form id="frm_add_place_coop" action="<%= request.getContextPath() %>/merchant/add_place_coop" method="post" style="padding-left: 40px;">
					<div style="width: 260px; float: left;">
						<select id="slt_source" size="5" style="width: 250px;">
							<optgroup label="公司名称&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;公司地址"></optgroup>
<%
	for (int i = 0; verifiedPlaceList != null && i < verifiedPlaceList.size(); i++) {
		place = verifiedPlaceList.get(i);
		if (coopIds.contains(place.getId())) {continue;}
%>
							<option value="<%= place.getId() %>"><%= place.getPlaceName() + "&nbsp;&nbsp;" + place.getAddress() %></option>
<%
	}
%>
						</select>
					</div>
					<div style="width: 100px; float: left;">
						<input class="fade_effect" id="btn_add_place_coop" type="button" value="选择->" style="width: 90px;"/>
					</div>
					<div style="width: 250px; float: left;">
						<select id="slt_destination" size="5" style="width: 250px;">
							<optgroup label="公司名称&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;公司地址"></optgroup>
<%
	for (int i = 0; placeList != null && i < placeList.size(); i++) {
		place = placeList.get(i);
		if (!"1".equals(place.getType())) {continue;}
%>
							<option value="<%= place.getId() %>">
							<%= place.getPlaceName() + "&nbsp;&nbsp;" + place.getAddress() %>
							</option>
<%
	}
%>
						</select>
						<input type="hidden" name="place_ids" id="place_ids"/>
					</div>
					<div style="clear: both;"></div>
					<div align="center">
						<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_confirm.png" alt="" style="margin-top: 20px;" onclick="document.getElementById('frm_add_place_coop').submit();"/>
						<img class="fade_effect" src="<%= request.getContextPath() %>/image/btn_cancel.png" alt="" style="margin-top: 20px;"
							onclick="restoreInitData();document.getElementById('div_add_place_coop').style.visibility = 'hidden';"/>
					</div>
					<span id="error_message"><%= msgCoopPlace == null ? "" : msgCoopPlace %></span>
				</form>
			</div>
		</div>
		<jsp:include page="../common/footer.jsp"></jsp:include>
	</body>
</html>